﻿import sys
from functools import partial
import logging 

from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

from network.network import ClientSocket
from network.package import *

from widgets.toolbars import BookToolBar, LibraryToolBar
from library import *

#logger = logging.getLogger(__name__)

class Library(QWidget):

    def __init__(self):
        super(Library, self).__init__()

        self.layout = QVBoxLayout()
        self.setLayout(self.layout)

        self.table = QTableWidget()
        self.layout.addWidget(self.table)

        self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.table.setEditTriggers(QAbstractItemView.NoEditTriggers)
        self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
        QTableWidget.resizeColumnsToContents(self.table)
        QTableWidget.resizeRowsToContents(self.table)

        self.table.setColumnCount(2)
        self.table.setHorizontalHeaderLabels(["书名", "作者"])

    def showBooks(self, bookList):

        self.table.setRowCount(len(bookList))
        self.books = bookList

        index = 0
        for bookInfo in bookList:

            print(bookInfo)
            #id = bookInfo["id"]
            title = bookInfo["title"].join(["《", "》"])
            authors = bookInfo["authors"]
            if not authors:
                authors = "佚名"

            self.table.setItem(index, 0, QTableWidgetItem(title))
            self.table.setItem(index, 1, QTableWidgetItem(authors))
            
            index += 1